package com.example.communitysystem.controller;

import com.example.communitysystem.dto.CommentCreateDTO;
import com.example.communitysystem.dto.CommentDTO;
import com.example.communitysystem.dto.ResultDTO;
import com.example.communitysystem.enums.CommentTypeEnum;
import com.example.communitysystem.exception.CustomizeErrorCode;
import com.example.communitysystem.exception.CustomizeException;
import com.example.communitysystem.mapper.CommentMapper;
import com.example.communitysystem.mapper.NotificationMapper;
import com.example.communitysystem.mapper.QuestionMapper;
import com.example.communitysystem.model.Comment;
import com.example.communitysystem.model.Question;
import com.example.communitysystem.dto.CommentDTO;
import com.example.communitysystem.dto.ResultDTO;
import com.example.communitysystem.exception.CustomizeErrorCode;
import com.example.communitysystem.mapper.CommentMapper;
import com.example.communitysystem.model.Comment;
import com.example.communitysystem.model.User;
import com.example.communitysystem.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
public class CommentController {

    private QuestionMapper questionMapper;
    private CommentMapper commentMapper;

    private CommentService commentService;
    private NotificationMapper notificationMapper;

    //获取前端传过来的json对象信息
    @RequestMapping(value = "/comment",method = RequestMethod.POST)
    @ResponseBody
    public Object post(@RequestBody CommentCreateDTO commentCreateDTO,
                       HttpServletRequest request
    ){
        ResultDTO resultDTO;

        //判断用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if(user == null){
            return ResultDTO.errorOf(CustomizeErrorCode.NO_LOGIN);
        }

        //判断评论内容是否为空
        if(commentCreateDTO == null || commentCreateDTO.getContent() == null || commentCreateDTO.getContent().equals("")){
            return new CustomizeException(CustomizeErrorCode.COMMENT_NOT_NULL);
        }

        //创建评论类对象
        Comment comment = new Comment();
        comment.setParentId(commentCreateDTO.getParentId());
        comment.setCommentContent(commentCreateDTO.getContent());
        comment.setType(commentCreateDTO.getType());
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setGmtModified(comment.getGmtCreate());
        comment.setCommentator(user.getId());
        commentService.insert(comment,user);

        //返回封装好的信息对象到前端
        resultDTO = ResultDTO.okOf();
        return resultDTO;
    }

    //获取二级评论，传入一级评论的id
    @ResponseBody
    @RequestMapping(value = "/comment/{id}", method = RequestMethod.GET)
    public ResultDTO<List> comments(@PathVariable(name = "id") Long id){
        List<CommentDTO> commentDTOList = commentService.getCommentDTOListByTargetId(id, CommentTypeEnum.COMMENT.getType());

        if (commentDTOList != null){
            return ResultDTO.okOf(commentDTOList);
        }

        return ResultDTO.errorOf(CustomizeErrorCode.SYS_ERROR);
    }


    @Autowired
    public void setQuestionMapper(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    @Autowired
    public void setCommentMapper(CommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    @Autowired
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    @Autowired
    public void setNotificationMapper(NotificationMapper notificationMapper) {
        this.notificationMapper = notificationMapper;
    }
}
